/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import nominalitzacions.domain.UrlDomainController;

/**
 *
 * @author daniel.camacho.rodriguez
 */
public class FirstRunView extends JFrame{


    private FirstRunViewController FRVC;
    private UrlDomainController UDC;

    private String msg0 = new String ("<html><p></p><p>Welcome to Nominalizatior</p>" +
            "<p></p><p>This wizard will help you to initialize your program.<p></p>" +
            " Only you have to follow these simple instructions.</p>" +
            "<p></p><p>For more information contact:</p><p>" +
            "</p><p>Sonia Sandalinas Perez</p><p>Marc Mauri Alloza</p" +
            "><p>Genis Solé Sorribes</p><p>Daniel Camacho Rodriguez</p></html>");
    private String msg1 = new String(/*"<html><p>To avoid trolls in your" +
            " program, set an administrator password.</p>" +
            "<p></p><p>Later you can refine your password." +
            "</p></html>"*/"<html><p></p><p></p><p></p>" +
            "<p></p><p><p>Defined password manager," +
            " please.</p><p></p></html>");
    private String msg2 = new String("<html><p></p><p>Defined language, please.</p></html>");
    private String msg3 = new String("<html><p></p><p></p><p></p><p></p><p></p><p></p>" +
            "<p>Thanks for using our nominalizer, hope you like it.</p></html>");

    private int vista = 0;

    private JFileChooser fc = new JFileChooser();
    private JPanel mainPanel = new JPanel();
    private JPanel logoPanel = new JPanel();
    private JPanel auxOnePanel = new JPanel();
    private JPanel auxTwoPanel = new JPanel();
    private JPanel auxTwoPanelOne = new  JPanel();
    private JPanel auxTwoPanelTwo = new  JPanel();
    private JPanel auxThreePanel = new JPanel();
    private JPanel auxThreePanelOne = new  JPanel();
    private JPanel auxThreePanelTwo = new  JPanel();
    private JPanel auxFourPanel = new JPanel();
    private JPanel buttomPanel = new JPanel();
    
    private JButton previous = new JButton("Previous");
    private JButton next = new JButton("Next");
    private JButton cancel = new JButton("Cancel");
    
    private JLabel infOne = new JLabel(msg0);
    private JLabel infTwo = new JLabel(msg1);
    private JLabel infThree = new JLabel(msg2);
    private JLabel infFour = new JLabel(msg3);

    private JLabel passMess = new JLabel("Password :");
    private JPasswordField pass = new JPasswordField();

    private JLabel languageName = new JLabel("Idiom :");
    private JTextField languageText = new JTextField();

    private JLabel infinitiveName = new JLabel("Infinitives path :");
    private JTextField infinitivePath = new JTextField();
    private JButton infinitiveBrowse = new JButton("Browse");

    private JLabel nounName = new JLabel("Nouns path :");
    private JTextField nounPath = new JTextField();
    private JButton nounBrowse = new JButton("Browse");

    private JLabel corpusName = new JLabel("Corpus path :");
    private JTextField corpusPath = new JTextField();
    private JButton corpusBrowse = new JButton("Browse");

    private JLabel irregularName = new JLabel("Irr. verbs path :");
    private JTextField irregularPath = new JTextField();
    private JButton irregularBrowse = new JButton("Browse");

    private JLabel image = new JLabel();

    public FirstRunView(FirstRunViewController FRVC){
        UDC = new UrlDomainController();
        this.FRVC = FRVC;
        iniComponents();
    }


    public void setVisible() {
        this.pack();
        this.setVisible(true);

    }
    
    public void setInvisible() {
        this.setVisible(false);
    }
    
    public void changeTo(int n) {
        this.vista = n;
        if (n == 0)
        {
            auxOnePanel.setVisible(true);
            auxTwoPanel.setVisible(false);
            auxThreePanel.setVisible(false);
            auxFourPanel.setVisible(false);

            mainPanel.add(auxTwoPanel, FlowLayout.CENTER);
            previous.setEnabled(false);
            next.setEnabled(true);
        }
        else if (n == 1)
        {
            auxOnePanel.setVisible(false);
            auxTwoPanel.setVisible(true);
            auxThreePanel.setVisible(false);
            auxFourPanel.setVisible(false);

            mainPanel.add(auxTwoPanel, FlowLayout.CENTER);
            previous.setEnabled(true);
            next.setEnabled(false);
            
        }
        else if (n == 2)
        {
            auxOnePanel.setVisible(false);
            auxTwoPanel.setVisible(false);
            auxThreePanel.setVisible(true);
            auxFourPanel.setVisible(false);

            mainPanel.add(auxThreePanel, BorderLayout.CENTER);
            previous.setEnabled(true);
            next.setText("Next");
            next.setEnabled(false);
        }
        else
        {
            auxOnePanel.setVisible(false);
            auxTwoPanel.setVisible(false);
            auxThreePanel.setVisible(false);
            auxFourPanel.setVisible(true);

            mainPanel.add(auxFourPanel, BorderLayout.CENTER);
            previous.setEnabled(true);
            next.setText("Accept");
            next.setEnabled(true);
        }
        this.pack();
        this.repaint();
    }

    private void actionPerformedPreviousButtom() {
        FRVC.previous(vista);
    }

    private void actionPerformedNextButtom(){
        if(vista == 1) try {
            FRVC.passIs(String.copyValueOf(pass.getPassword()));
        } catch (IOException ex) {
            String msg = ex.getMessage();
            JOptionPane.showMessageDialog(this, msg, "Error writing",
                        JOptionPane.ERROR_MESSAGE);
        }
        if(vista == 2) {
            try {
                FRVC.setLanguage(languageText.getText(),
                        infinitivePath.getText(), irregularPath.getText(),
                        nounPath.getText(), corpusPath.getText());
            } catch (IOException ex) {
                String msg = ex.getMessage();
                JOptionPane.showMessageDialog(this, msg, "Error writing",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
        if(vista == 3) {
            FRVC.closeFirstRun();
            try {
                FRVC.showPrincipalView();
            } catch (IOException ex) {
                Logger.getLogger(FirstRunView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else try {
            FRVC.next(vista);
        } catch (FileNotFoundException ex ) {
            Logger.getLogger(FirstRunView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FirstRunView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void actionPerformedCancelButtom() {
        FRVC.closeFirstRun();
    }

    private void actionPerformedPassEdit() {
        String nll = "";
        if(nll.equals(String.copyValueOf(pass.getPassword())))
        {
            next.setEnabled(false);
        }
        else
        {
            next.setEnabled(true);
        }
        this.pack();
        this.repaint();
    }

    private void actionPerformedPathButtons(ActionEvent event, String msg) {
        int returnVal = fc.showDialog(this, "Choose " + msg);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (event.getSource() == infinitiveBrowse) {
                infinitivePath.setText(file.getAbsolutePath());
            } else if (event.getSource() == corpusBrowse) {
                corpusPath.setText(file.getAbsolutePath());
            } else if (event.getSource() == irregularBrowse) {
                irregularPath.setText(file.getAbsolutePath());
            } else {
                nounPath.setText(file.getAbsolutePath());
            }
            this.pack();
            this.repaint();
        }
    }

    private boolean check(){
        String res = new String("");
        if (res.equals(languageText.getText())) return false;
        else if (res.equals(infinitivePath.getText())) return false;
        else if (res.equals(irregularPath.getText())) return false;
        else if (res.equals(nounPath.getText())) return false;
        else if (res.equals(corpusPath.getText())) return false;
        else return true;
    }

    private void actionPerformedPathEdit(){
        if(check())
        {
            next.setEnabled(true);
        }
        else
        {
            next.setEnabled(false);
        }
    }


    private void iniComponents() {
        iniFrame();
        iniMainPanel();
        assignListeners();
    }

    private void iniFrame() {
        setMinimumSize(new Dimension(640, 480));
        this.setLocationRelativeTo(null);
        this.setPreferredSize(this.getMinimumSize());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("First Boot");
        setResizable(false);
        JPanel contentPane = (JPanel) this.getContentPane();
        contentPane.add(mainPanel);
    }

    private void iniMainPanel() {

        buttomPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
        previous.setPreferredSize(new Dimension(100, 25));
        previous.setEnabled(false);
        buttomPanel.add(previous);
        next.setPreferredSize(new Dimension(100, 25));
        buttomPanel.add(next);
        cancel.setPreferredSize(new Dimension(100, 25));
        buttomPanel.add(cancel);

        logoPanel.setPreferredSize(new Dimension(600, 100));
//        ImageIcon imageLogo = new ImageIcon("C:/User/Irene/Documents/NetbeansProjects/JIJU/image.PNG");
//        JLabel x = new JLabel(imageLogo, JLabel.CENTER);
//        logoPanel.add(x);
//        String dir = UDC.getRes()+"image.PNG";
//        image.setIcon(new ImageIcon("C:/User/Irene/Documents/NetbeansProjects/JIJU/image.PNG"));
//        System.out.println(UDC.getRes()+"image.png");
//        logoPanel.add(image);
        
        // One Panel : Infor
        auxOnePanel.setLayout(new FlowLayout());
        auxOnePanel.add(infOne);

        // Two Panel : definir Contraseña        
        GroupLayout layout = new GroupLayout(auxTwoPanelTwo);
        auxTwoPanelTwo.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup heightGroup = layout.createSequentialGroup();
        heightGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
                .addComponent(passMess));
        heightGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
                .addComponent(pass));
        layout.setHorizontalGroup(heightGroup);

        GroupLayout.SequentialGroup verGroup = layout.createSequentialGroup();
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(passMess)
                .addComponent(pass));
        layout.setVerticalGroup(verGroup);

        auxTwoPanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));
        auxTwoPanelOne.add(infTwo);

        auxTwoPanel.setLayout(new BorderLayout());
        auxTwoPanel.add(auxTwoPanelOne, BorderLayout.NORTH);
        auxTwoPanel.add(auxTwoPanelTwo, BorderLayout.CENTER);

        // Three Panel : definir Idioma

        languageText.setPreferredSize(new Dimension(60,25));
        infinitivePath.setPreferredSize(new Dimension(60, 25));
        irregularPath.setPreferredSize(new Dimension(60, 25));
        nounPath.setPreferredSize(new Dimension(60, 25));
        corpusPath.setPreferredSize(new Dimension(60, 25));

        GroupLayout layoutt = new GroupLayout(auxThreePanelTwo);
        auxThreePanelTwo.setLayout(layoutt);
        layoutt.setAutoCreateGaps(true);
        layoutt.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup hGroup = layoutt.createSequentialGroup();
        hGroup.addGroup(layoutt.createParallelGroup()
            .addComponent(languageName)
            .addGap(25)
            .addComponent(infinitiveName)
            .addComponent(irregularName)
            .addComponent(nounName)
            .addComponent(corpusName));
        hGroup.addGroup(layoutt.createParallelGroup()
            .addComponent(languageText)
            .addGap(25)
            .addComponent(infinitivePath)
            .addComponent(irregularPath)
            .addComponent(nounPath)
            .addComponent(corpusPath)
            .addGap(25));
        hGroup.addGroup(layoutt.createParallelGroup()
            .addComponent(infinitiveBrowse)
            .addComponent(irregularBrowse)
            .addComponent(nounBrowse)
            .addComponent(corpusBrowse)
            .addGap(25));
        layoutt.setHorizontalGroup(hGroup);

        GroupLayout.SequentialGroup vGroup = layoutt.createSequentialGroup();
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addComponent(languageName)
            .addComponent(languageText));
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addGap(25)
            .addGap(25));
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addComponent(infinitiveName)
            .addComponent(infinitivePath)
            .addComponent(infinitiveBrowse));
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addComponent(irregularName)
            .addComponent(irregularPath)
            .addComponent(irregularBrowse));
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addComponent(nounName)
            .addComponent(nounPath)
            .addComponent(nounBrowse));
        vGroup.addGroup(layoutt.createParallelGroup(Alignment.BASELINE)
            .addComponent(corpusName)
            .addComponent(corpusPath)
            .addComponent(corpusBrowse));
        layoutt.setVerticalGroup(vGroup);

        auxThreePanelOne.setLayout(new FlowLayout(FlowLayout.CENTER));
        auxThreePanelOne.add(infThree);

        auxThreePanel.setLayout(new BorderLayout());
        auxThreePanel.add(auxThreePanelOne, BorderLayout.NORTH);
        auxThreePanel.add(auxThreePanelTwo, BorderLayout.CENTER);

        //Four Panel: info final
        auxFourPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        auxFourPanel.add(infFour);


        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(logoPanel, BorderLayout.NORTH);
        mainPanel.add(auxOnePanel, BorderLayout.CENTER);
        mainPanel.add(buttomPanel, BorderLayout.SOUTH);

        //setContentPane(mainPanel);

    }

    private void assignListeners() {

        previous.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedPreviousButtom();
            }
        });

        next.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedNextButtom();
            }
        });

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedCancelButtom();
            }
        });

        pass.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
        });

        infinitiveBrowse.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedPathButtons(event, "infinitives");
            }
        });

        corpusBrowse.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedPathButtons(event, "corpus");
            }
        });

        irregularBrowse.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedPathButtons(event, "irregular verbs");
            }
        });

        nounBrowse.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedPathButtons(event, "nouns");
            }
        });

        languageText.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
        });

        infinitivePath.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
        });

        irregularPath.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
        });

        nounPath.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
        });

        corpusPath.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPathEdit();
            }
        });

    }
}
